﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;

public class UIPlantCard : MonoBehaviour,IPointerEnterHandler,IPointerExitHandler,IPointerClickHandler
{
    //遮罩的image
    private Image maskImg;
    //cd
    public float cooltime;
    //剩余cd
    private float currTimeForCD;
    //能否种植物的判断
    private bool canPlace;
    //种植的状态
    private bool wantPlace;
    //鼠标植物
    private GameObject plant;
    //预览植物
    private GameObject plantInGrid;
    public bool CanPlace
    {
        get => canPlace; set
        {
            canPlace = value;
            if (!canPlace)
            {
                maskImg.fillAmount = 1;
                CDEnter();
            }
            else
            {
                maskImg.fillAmount = 0;
            }
        }
    }

    public bool WantPlace { get => wantPlace; 
        set {
            wantPlace = value;
            if (wantPlace)
             {
                GameObject prefab = PlantManager.Instance.GetPlantForType(PlantType.Sunflower);
                plant = GameObject.Instantiate<GameObject>(prefab, Vector3.zero, Quaternion.identity, PlantManager.Instance.transform);
                plant.GetComponent<Sunflower>().InitForCreate(false);

             }
            else
            {
                if (plant!=null)
                {
                  Destroy(plant.gameObject);
                  plant = null;
                }
                
            }
         }
    }

    void Start()
    {
        maskImg = transform.Find("Mask").GetComponent<Image>();
        CanPlace = false;
    }

    private void Update()
    {
        if (WantPlace && plant != null)
        {
            Vector3 point = Camera.main.ScreenToWorldPoint(Input.mousePosition);
            plant.transform.position = new Vector3(point.x, point.y,0);

            if (Vector2.Distance(point, GridManager.Instance.GetPointByMouse())<1.5)
            {
                if (plantInGrid==null)
                {
                    plantInGrid = GameObject.Instantiate<GameObject>(plant, GridManager.Instance.GetPointByMouse(), Quaternion.identity, PlantManager.Instance.transform);
                    plantInGrid.GetComponent<Sunflower>().InitForCreate(true);
                }
                else
                {
                    plantInGrid.transform.position = GridManager.Instance.GetPointByMouse();
                }
            }
            else
            {
                if(plantInGrid=null){
                    Destroy(plantInGrid.gameObject);
                    plantInGrid = null;
                }
                
            }
            if (Input.GetMouseButtonDown(0))
            {
                plant.transform.position = GridManager.Instance.GetPointByMouse();
                plant.GetComponent<Sunflower>().InitForPlace();
                plant = null;
                Destroy(plantInGrid.gameObject);
                plantInGrid = null;
                WantPlace = false;

            }
               
        }
    }

    private void CDEnter()
    {

        StartCoroutine(calCD());
    }

    IEnumerator calCD()
    {
        float calCD = (1 / cooltime) * 0.1f;
        currTimeForCD = cooltime;
        while (currTimeForCD>=0)
        {
            yield return new WaitForSeconds(0.1f);
            maskImg.fillAmount -= calCD;
            currTimeForCD -= 0.1f;
        }

        
        CanPlace = true;

    }
    public void OnPointerEnter(PointerEventData eventData)
    {
        if (!CanPlace) return;
        transform.localScale = new Vector2(1.05f, 1.05f);
    }

    public void OnPointerExit(PointerEventData eventData)
    {
        if (!CanPlace) return;
        transform.localScale = new Vector2(1f, 1f);
    }


    public void OnPointerClick(PointerEventData eventData)
    {
        if (!CanPlace) return;
        if (!WantPlace)
        {
            WantPlace = true;
        }
    }
}
